#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
    int longestSquareStreak(vector<int>& nums) {
        sort(nums.begin(), nums.end());
        int n = nums.size();

        unordered_map<int, int> hash;
        for (auto& e : nums)
        {
            hash[e] = 1;
        }

        int ret = 0;
        for (int i = 1; i < n; ++i)
        {
            int fi = sqrt(nums[i]);

            if (fi == sqrt(nums[i]) && hash.count(fi))
            {
                hash[nums[i]] = hash[fi] + 1;
            }

            ret = max(ret, hash[nums[i]]);
        }

        return ret < 2 ? -1 : ret;
    }
};